<?php

namespace api\modules\v1\controllers\clinic;

use Yii;
use api\controllers\OnAuthController;
use common\models\clinic\Doctor;
use common\helpers\CommonHelper;
use services\clinic\ClinicDoctorService;

/**
 * Class DoctorController
 * @package api\modules\v1\controllers\clinic
 */
class DoctorController extends OnAuthController
{
    /**
     * @var Doctor
     */
    public $modelClass = Doctor::class;


    /**
     * @api {get} /v1/clinic/doctor/list 医生列表
     * @apiName list
     * @apiGroup ClinicDoctor
     *
     * @apiParam {Number} page                          页码
     * @apiParam {Number} pageSize                      每页显示数量
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  当前页码
     * @apiSuccess {Number}  data.pageSize              每页显示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  诊室列表
     * @apiSuccess {Number}  data.list.id               医生ID
     * @apiSuccess {Number}  data.list.project_id       项目ID
     * @apiSuccess {Number}  data.list.clinic_id        诊室ID
     * @apiSuccess {String}  data.list.name             诊室名称
     * @apiSuccess {String}  data.list.clinic_name      诊室名
     * @apiSuccess {Number}  data.list.status           状态
     * @apiSuccess {String}  data.list.status_str       状态值
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 0;
        $pageSize = $params['pageSize'] ?? 0;

        // 查询结果
        $clinicDoctorService = new ClinicDoctorService();
        return $clinicDoctorService->getListPage($params, (int)$page, (int)$pageSize);
    }

    /**
     * @api {get} /v1/clinic/doctor/info 诊室详情
     * @apiName info
     * @apiGroup ClinicDoctor
     *
     * @apiParam {Number} id                            医生ID
     *
     * @apiSuccess {Object}     data                    返回数据
     * @apiSuccess {Number}     data.id                 医生ID
     * @apiSuccess {Number}     data.clinic_id          诊室ID
     * @apiSuccess {String}     data.name               诊室名称
     * @apiSuccess {Number}     data.status             状态
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        $clinicDoctorService = new ClinicDoctorService();
        return $clinicDoctorService->getDoctorInfo($id);
    }

    /**
     * @api {post} /v1/clinic/doctor/create 新增医生
     * @apiName create
     * @apiGroup ClinicDoctor
     *
     * @apiParam {Number} clinic_id             诊室ID【选填】
     * @apiParam {String} name                  医生名称
     * @apiParam {Number} status                状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id            医生ID
     */
    public function actionCreateDoctor()
    {
        try {
            // $transaction = Yii::$app->db->beginTransaction();

            $params = $this->post;

            $clinicDoctorService = new ClinicDoctorService();
            $data = $clinicDoctorService->create($params);

            // $transaction->commit();
        } catch (\Exception $e) {
            // $transaction->rollBack();

            $data = CommonHelper::throwRes($e);
        }

        return $data;
    }

    /**
     * @api {post} /v1/clinic/doctor/update 编辑医生
     * @apiName update
     * @apiGroup ClinicDoctor
     *
     * @apiParam {Number} id                    医生ID
     * @apiParam {Number} clinic_id             诊室ID【选填】
     * @apiParam {String} name                  医生名称
     * @apiParam {Number} status                状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id            医生ID
     */
    public function actionUpdateDoctor()
    {
        try {
            // $transaction = Yii::$app->db->beginTransaction();

            $params = $this->post;

            $clinicDoctorService = new ClinicDoctorService();
            $data = $clinicDoctorService->edit($params);

            // $transaction->commit();
        } catch (\Exception $e) {
            // $transaction->rollBack();

            $data = CommonHelper::throwRes($e);
        }

        return $data;
    }
}
